package core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

import utility.Parser;
import utility.WriteFile;

public class StaccatoInterface {
	
	private int[][] matrice;
	private ArrayList<String> nomiColonne;

	public StaccatoInterface(ArrayList<Set<Node>> B) throws IOException{
		
		nomiColonne=new ArrayList<String>();
		setColonne(B);
		
		matrice = new int[B.size()][nomiColonne.size()];
		
		
		//System.out.println(stampaMatrice(B));
		
		
		for(int i=0; i< B.size(); i++){ // numero della riga
			for(Node n : B.get(i))
				matrice[i][getColonna(n.getId())]=1;
		}
		//System.out.println("E dopo...");
		//System.out.println(stampaMatrice(B));
		
		WriteFile wf = new WriteFile("temp_staccato");
		wf.writeFile(stampaMatrice(B));
		wf.closeFile();

		 		
	}
	
	public Set<ArrayList<String>> staccato() throws IOException{

	    String s = null;
        Set<ArrayList<String>> totalResult = new HashSet<ArrayList<String>>();

        
        int righe=0;
        try {

            //System.out.println("./staccato -o"+this.nomiColonne.size()+"temp_staccato");
            Process p = Runtime.getRuntime().exec("./staccato.exe -o "+this.nomiColonne.size()+" temp_staccato");

            BufferedReader stdInput = new BufferedReader(new
                 InputStreamReader(p.getInputStream()));
 
            BufferedReader stdError = new BufferedReader(new
                 InputStreamReader(p.getErrorStream()));

            Parser parser = new Parser();
    		

            ArrayList<String> result = new ArrayList<String>();
            totalResult = new HashSet<ArrayList<String>>();
            while ((s = stdInput.readLine()) != null) {
            	result = new ArrayList<String>();
            	ArrayList<Integer> res = parser.parseDiagnosi(s);
            	if(res.size()>0)
            	for(int i=0; i<res.size(); i++)
            		result.add(this.nomiColonne.get(res.get(i)-1));
            	righe++;
            	totalResult.add(result);
            	//System.out.println(totalResult);
            }

        }
        catch (IOException e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }

        return totalResult;
	}
	

	
	public String stampaMatrice(ArrayList<Set<Node>> B){
		String str="";
		for(int i=0; i<this.nomiColonne.size(); i++)
			str=str+nomiColonne.get(i)+"\t";
		//System.out.println(str);
		str="";
		for(int i=0; i<B.size(); i++){
			for(int j=0; j<nomiColonne.size(); j++){
				str=str+matrice[i][j]+" ";
			}
			if(i<B.size()-1){
				str=str+"-\n";
			}else{
				str=str+"-";
			}
		}
		return str;
	}
	
	public int getColonna(String id){
		for(int i=0; i< nomiColonne.size(); i++)
			if(id.equalsIgnoreCase(nomiColonne.get(i)))
				return i;
		//System.err.print("Qualcosa non va");
		return 88;
	}
	
	
	public void setColonne(ArrayList<Set<Node>> B) throws IOException{

		for(int i=0; i<B.size(); i++){
			for(Node n: B.get(i)){
				if(!nomiColonne.contains(n.getId()));
				nomiColonne.add(n.getId());	
			}
		}
		//System.out.println("Le colonne sono");
		String str="";
		for(int i=0; i<nomiColonne.size(); i++)
			str=str+nomiColonne.get(i)+" ";
	//	System.out.println(str+"\n");
			
	}
}
